#include "subtasktable.h"

subTaskTable::subTaskTable(QObject *parent) : QObject(parent)
{

    m_table = nullptr;
}

subTaskTable::subTaskTable(QTableWidget *table, QObject *parent):QObject(parent)
{

    m_table = table;
    m_table->clear();
    m_table->setColumnCount(5);
    // 隐藏行号
    QHeaderView *header = m_table->verticalHeader();
    header->setHidden(true);

    m_table->setColumnHidden(ID, true);
    m_table->setColumnWidth(STATE, 35);
//    m_table->setColumnWidth(NAME, 100);
    m_table->setColumnWidth(TOMATO, 50);


    // 以行为单位选中
    m_table->setSelectionBehavior ( QAbstractItemView::SelectRows);
    m_table->setSelectionMode ( QAbstractItemView::SingleSelection);
//    _table->setSelectionMode ( QAbstractItemView::NoSelection);

    m_table->setFont(QFont("宋体"));
    //设置表格中每一行的表头
    m_table->setHorizontalHeaderLabels(QStringList() << "状态"<< "ID" <<"任务名" << "番茄数"  << "已完成" );
    //设置表格数据区内的所有单元格都不允许编辑
    m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);

//    m_table->setShowGrid(true);
    m_table->setWindowFlags(Qt::FramelessWindowHint);

    // 初始化图标
    QSize size(15,15);
    m_pix_add = QPixmap(":/pics/add.png").scaled(size,  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    m_pix_delete = QPixmap(":/pics/delete.png").scaled(size,  Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    m_pix_ready = QPixmap(":/pics/ready.png").scaled(size);
    m_pix_not_ready = QPixmap(":/pics/not_ready.png").scaled(size);

}

void subTaskTable::showSubTask(taskItem &task)
{

    m_currentTaskId = task.m_id;
    m_table->setRowCount(0);

    int row=0;
    for (auto i=task.m_subTaskMap.constBegin(); i != task.m_subTaskMap.constEnd(); ++i){
        if (!i->name.isEmpty()){
            addSubTask(row++, *i);
        }
    }
}



void subTaskTable::setCurrentSubTaskTable(taskItem &taskitem)
{

    showSubTask(taskitem);

}



void subTaskTable::addSubTask(int newRow, const subTask &subtask)
{

    m_table->insertRow(newRow);
    m_table->setRowHeight(newRow, 15);

    QTableWidgetItem * widget_item;

    // state
    QLabel *state = new QLabel();
    // 设置鼠标在label上的形状
    bool select_flag = false;
    state->setCursor(Qt::PointingHandCursor);
    if (m_table->rowCount() == 1){
        state->setPixmap(m_pix_ready);
        select_flag = true;
        emit selectCurrentSubTask(subtask);
    } else{
        state->setPixmap(m_pix_not_ready);
    }

    state->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    state->setStatusTip("unselected");
    m_table->setCellWidget(newRow, STATE, state);


    // id

    widget_item = new QTableWidgetItem(QString("%1").arg(subtask.id));
    widget_item->setToolTip(subtask.describe);
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,ID, widget_item);

    // name

    widget_item = new QTableWidgetItem(QString(subtask.name));
    widget_item->setToolTip(subtask.describe);
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,NAME, widget_item);



    // tomatoes

    widget_item = new QTableWidgetItem(QString("%1").arg(subtask.tomato));
    widget_item->setToolTip("子任务/步骤 所需的番茄总数");
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,TOMATO, widget_item);
    // achieve
    widget_item = new QTableWidgetItem(QString("%1").arg(subtask.achieve));
    widget_item->setToolTip("已完成的番茄总数");
    widget_item->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_table->setItem(newRow,ACHIEVE, widget_item);







    if (select_flag) state->setStatusTip("selected");
}
